package cn.xiyun.xiyuncc.MQ.consumer;

import cn.xiyun.xiyuncc.domain.CourseOrder;
import cn.xiyun.xiyuncc.service.ICourseOrderService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        consumerGroup = "Order_TimeOut_group",
        topic = "OrderTimeOut_topic",
        selectorExpression = "OrderTimeOut_tags",
        messageModel = MessageModel.BROADCASTING
)
public class OrderTimeOutConsumer implements RocketMQListener<String> {
    @Autowired
    private ICourseOrderService courseOrderService;

    @Override
    public void onMessage(String orderNo) {
        System.out.println(orderNo);
        Wrapper<CourseOrder> wrapper = new EntityWrapper<>();
        wrapper.eq("order_no", orderNo);
        // 根据订单号查询 订单
        CourseOrder courseOrder = courseOrderService.selectOne(wrapper);
        if (courseOrder == null) return;
        // 判断订单状态,未支付则自动取消
        if (courseOrder.getStatusOrder() == CourseOrder.ORDER_UNPAID) {
            courseOrder.setStatusOrder(CourseOrder.ORDER_AUTO_CANCEL);
            courseOrderService.updateById(courseOrder);
        }
    }
}
